package com.gs.wzmanager.service;

import com.gs.wzmanager.config.datasource.DatasourceKey;
import com.gs.wzmanager.config.datasource.TargetDataSource;
import com.gs.wzmanager.dao.ForecastContrastDao;
import com.gs.wzmanager.entity.ForecastContrast;
import com.gs.wzmanager.utils.Constant;
import com.gs.wzmanager.utils.DateUtil;
import com.gs.wzmanager.vo.ForecastContrastVO;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.FutureTask;

/**
 * Created with IntelliJ IDEA.
 * User: Legolas
 * Date: 2017/11/15 0015
 */
@Service
@TargetDataSource(DatasourceKey.DB2)
public class ForeCastConstructService {

    @Autowired
    ForecastContrastDao forecastContrastDao;

    public List<ForecastContrastVO> getContrustData() throws Exception {
        List<ForecastContrastVO> forecastContrastVOs = new LinkedList<>();

        /*Dynamic数据源中使用FutureTask报错*/
        //FutureTask<List<ForecastContrast>> actualTask = new FutureTask(()->getActual());
        FutureTask<List<ForecastContrast>> forecastTask = new FutureTask(() -> getForecast());
        new Thread(forecastTask).start();

        List<ForecastContrast> actuals = forecastContrastDao.queryYesterdayActual();

        for (int i = 0; i < actuals.size(); i++) {
            ForecastContrast forecastContrast = actuals.get(i);
            forecastContrast.setMaxT(Integer.parseInt(forecastContrast.getMaxT()) / 10 + "." + Integer.parseInt(forecastContrast.getMaxT()) % 10);
            forecastContrast.setMinT(Integer.parseInt(forecastContrast.getMinT()) / 10 + "." + Integer.parseInt(forecastContrast.getMinT()) % 10);
            forecastContrast.setRx(Integer.parseInt(forecastContrast.getRx()) / 10 + "." + Integer.parseInt(forecastContrast.getRx()) % 10);
        }

        List<ForecastContrast> forecasts = forecastTask.get();

        System.out.println(actuals);
        System.out.println(forecasts);

        if (actuals != null) {
            for (int i = 0; i < actuals.size(); i++) {
                ForecastContrastVO forecastContrastVO = new ForecastContrastVO();
                String station = actuals.get(i).getStation();
                forecastContrastVO.setStation(station);
                forecastContrastVO.setActualMax(actuals.get(i).getMaxT());
                forecastContrastVO.setActualMin(actuals.get(i).getMinT());
                forecastContrastVO.setActualRx(actuals.get(i).getRx());
                for (int j = 0; j < forecasts.size(); j++) {
                    if (station.equals(forecasts.get(j).getStation())) {
                        forecastContrastVO.setForeMax(forecasts.get(j).getMaxT());
                        forecastContrastVO.setForeMin(forecasts.get(j).getMinT());
                        forecastContrastVO.setForeRx(forecasts.get(j).getRx());
                    }
                }
                forecastContrastVOs.add(forecastContrastVO);
            }
            return forecastContrastVOs;
        } else return null;
    }


    private List<ForecastContrast> getForecast() throws Exception {
        List<ForecastContrast> forecastContrasts = new ArrayList<>();
        Map dby = DateUtil.getYearMonthDayBeforeYesterday();
        String rootPath = Constant.FORECAST_CONTRAST.COUNTRY_FORECAST_FILE + dby.get("year") + "/" + dby.get("year") + dby.get("month") + "/" + dby.get("year") + dby.get("month") + dby.get("day") + "/";
        File rootFile = new File(rootPath);
        File file;
        BufferedReader br;
        if (rootFile.exists()) {
            File[] files = rootFile.listFiles();
            file = files[files.length - 1];
        } else return null;
        if (file.exists()) {
            try {
                br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
                String line;
                ForecastContrast forecastContrast = null;
                while ((line = br.readLine()) != null) {
                    line = line.trim();
                    if (line.startsWith("586") || line.startsWith("587")) {
                        String[] lines = line.split(" ");
                        forecastContrast = new ForecastContrast();
                        forecastContrast.setStation(lines[0]);
                    }
                    if (line.startsWith("24") && forecastContrast != null) {
                        String[] lines = line.split(" ");
                        forecastContrast.setMaxT(lines[12]);
                        forecastContrast.setMinT(lines[14]);
                        forecastContrast.setRx(lines[23]);
                        forecastContrasts.add(forecastContrast);
                        continue;
                    }
                }
                return forecastContrasts;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }


    private List<ForecastContrast> getForecastTest() throws Exception {
        List<ForecastContrast> forecastContrasts = new ArrayList<>();
        Map dby = DateUtil.getYearMonthDayBeforeYesterday();
        String rootPath = Constant.FORECAST_CONTRAST.COUNTRY_FORECAST_FILETEST + dby.get("year") + dby.get("month") + dby.get("day") + "/";
        File rootFile = new File(rootPath);
        File file = null;
        BufferedReader br;

        if (rootFile.exists()) {
            File[] files = rootFile.listFiles();
            file = files[files.length - 1];
        }
        if (file.exists()) {
            try {
                br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
                String line;
                ForecastContrast forecastContrast = null;
                while ((line = br.readLine()) != null) {
                    line = line.trim();
                    if (line.startsWith("586") || line.startsWith("587")) {
                        String[] lines = line.split(" ");
                        forecastContrast = new ForecastContrast();
                        forecastContrast.setStation(lines[0]);
                    }
                    if (line.startsWith("24") && forecastContrast != null) {
                        String[] lines = line.split(" ");
                        forecastContrast.setMaxT(lines[12]);
                        forecastContrast.setMinT(lines[14]);
                        forecastContrast.setRx(lines[23]);
                        forecastContrasts.add(forecastContrast);
                        continue;
                    }
                }
                return forecastContrasts;
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

}




